package com.caj.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.caj.client.StockClient;
import com.caj.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;

@RestController
@RefreshScope
public class OrderController {

    // restTemplate是启动类里构建的！
    @Autowired
    private RestTemplate restTemplate;

    @Value("${info:empty}")
    private String info;

    @Autowired
    private StockClient stockClient;

    @GetMapping("/order/test")
    public String test(){
        //1、 直接访问库存服务的/stock/test接口，获取数据
        String str = restTemplate.getForObject("http://stock/stock/test", String.class);

        //2、 响应数据
        return "Order test get " + str;
    }

    @GetMapping("/order/info")
    @SentinelResource(value = "info")
    public String info(){
        return info;
    }

    // 基于OpenFeign，尝试访问Stock服务
    @GetMapping("/order/feign")
    public String feign(){
        String result = stockClient.test();
        return result;
    }

    //----------------------关联------------------
    @GetMapping("/order/add")
    @SentinelResource(value = "add")
    public String add() {
        return "add";
    }

    //----------------------链路------------------
    @Autowired
    private OrderService orderService;

    @GetMapping("/order/aaa")
    @SentinelResource(value = "aaa")
    public String aaa() {
        orderService.common();
        return "aaa";
    }
    @GetMapping("/order/bbb")
    @SentinelResource(value = "bbb")
    public String bbb() {
        orderService.common();
        return "bbb";
    }

    //-----------------并发线程数---------------
    @GetMapping("/order/bingfa")
    @SentinelResource(value = "bingfa")
    public String bingfa() {
        // ============并发线程数==============
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return info;
    }

    // =================================熔断降级===========================
    @GetMapping("/order/circuitbreaker")
    @SentinelResource(value = "circuitbreaker")
    public String circuitbreaker(String value) throws InterruptedException {
        switch (value){
            case "1":
                Thread.sleep(1000);
                break;
            case "2":
                int i = 1 / 0;
        }

        return "circuitbreaker - success!";
    }

    // =================================熔断降级(包含降级方法)===========================
    @GetMapping("/order/sentinel")
    @SentinelResource(value = "sentinel",blockHandlerClass = OrderControllerBlock.class,blockHandler = "sentinelBlock"
    ,fallbackClass = OrderControllerFallback.class, fallback = "sentinelFallback")
    public String sentinel(@RequestParam String value) throws InterruptedException {
        // 业务代码………………
        switch (value){
            case "1":
                Thread.sleep(1000);
                break;
            case "2":
                int i = 1 / 0;
        }
        return "sentinel - success!";
    }

    //==================热点规则1=======================
    @GetMapping("/order/hot1")
    @SentinelResource(value = "hot1")
    public String hot1(String userId,Integer type){
        return "userId:" + userId + ",type:" + type;
    }

    //==================热点规则2=======================
    @GetMapping("/order/hot2/{userId}")
    @SentinelResource(value = "hot2")
    public String hot2(@PathVariable String userId, Integer type){
        return "userId:" + userId + ",type:" + type;
    }

    //==================授权规则=======================
    @GetMapping("/order/author")
    @SentinelResource(value = "author")
    public String author(){
        return "author!";
    }

    //-----------------------Gateway的Filter测试------------------
    @GetMapping("/order/gateway")
    public String gateway(HttpServletRequest request){
        return "gateway";
    }

}
